<?php
require_once("inc.member.php");

$this_title="$vars[title] &raquo; ".__("Sponsor Paradigm");
$page_title=__("Sponsor Paradigm");
$content_title=__("Sponsor Paradigm");

$r_status=array("normal","unverified","suspended","terminated");
$r_status_d=array("normal"=>__("Normal"),"unverified"=>__("Unverified"),"suspended"=>__("Suspended"),"terminated"=>__("Terminated"));

require_once('inc.genealogy_tab.php');

$this_month['start'] = date('Y-m-d H:i:s', mktime(0,0,0,ndate('n'),1,2013));
$this_month['end'] = date('Y-m-d H:i:s', mktime(23,59,59,ndate('n'),0,2015));

//##### AJAX CALL #####
//downline child request
if($get_s['aj'] && $_GET["gen_tree"]){
	$user = get_user_detail_by_id($get_d['id']);
	if($user){
		$downline_node = retrieve_gen_downline($user['id'], 'ref', 'gen_tree', 2);
	}
	print $downline_node;
	exit;
}
//##### END AJAX CALL #####



/*##### FORM POST #####*/
if($_POST["__req"]){

 if(!$errmsg){
	//check password
  	if(!strlen($post_s["authcode"])){
   		$errmsg.=replace_tag(__("'<%field%>' is a required field."), array("<%field%>"=>__("AuthCode")))."<br />\n";
  	}else{
   		$enc_pass=explode(":", $r_user["ewallet_enc_password"]);
   		if(md5($post_s["authcode"].$enc_pass[1]) != $enc_pass[0]){
    		$errmsg.=__("You have entered an invalid AuthCode.")."<br />\n";
   		}else{
   			$_SESSION['show_tree'] = 1;
   		}
  	}
  
 }
 
 $errmsg=$errmsg? format_err($errmsg) : "";

}

//#####GET DOWNLINE
$r_upline=$r_user;
$upline_id=$uid;
$which_network = 'ref';
if($_GET['cur_uid']!=""){
	$upline_code = $_GET['cur_uid'];
	$r_upline=get_user_detail_by_code($upline_code);
	if(!$r_upline || !is_group_ref_downline_of($r_upline["id"], $uid)){
		//$r_upline=0;
		$upline_id=$uid;
	}else{
		$upline_id=$r_upline["id"];
	}
}
$downline_node = retrieve_gen_downline($upline_id, 'ref', 'gen_tree', 2);

$head = $r_upline;

//ps
$this_month_sales_maintain = calc_personal_sales($head['id'], $this_month['start'], $this_month['end']);
$strlen = strlen($head['name']);
$head_width=$vars['gen_tweak']['base_info_width'] + ($strlen * ($vars['gen_tweak']['char_width']) + 10) + 100;
if($upline_id>0){
$head_node = "<span class='gen_id'>".$head["code"]."</span>".
"<span class='gen_jdate'>".__('Rank').": ".__($vars['rank_index_a_d'][$head["rank_a"]])."</span>".
"<span class='gen_pspgs'>".__('Group Sales').": ".number_format($this_month_sales_maintain, 2)."</span>";
}
$downline_tree=__("Sponsor").": <select name='cur_uid' id='cur_uid' onchange=\"window.location='$this_file?cur_uid='+document.getElementById('cur_uid').value;\" >".get_sponsor_downline($uid, $_GET['cur_uid'])."</select></p>"."
<div id='genealogy_tree' class='tree'>
 <ul>
  <li class='root' id='0' style='width:{$head_width}px;'><div class='tree-node".(USER_BROWSER == 'msie'? "-ie" : '')."'>$head_node</div><div class='clear'></div>
   <ul>
	$downline_node
   </ul>
  </li>
 </ul>
</div>";

if($_SESSION['show_tree']=='1'){
$downline_box=
"<br>
<div class='genealogy'>
	$downline_tree
</div>";
}else{

$downline_box=($errmsg || $msg? 	"$errmsg $msg" : "")."
<form name='tree_form' method='post' action='$this_file'>


	<input type='hidden' name='__req' value='1' />

	<table class='pbt_table'>

	 <tr>
	
	  <td width='$td_width'>AuthCode:</td>
	
	  <td><input type='text' name='authcode' $inputbox_style /></td>    
	
	 </tr>

	 <tr>
	
	  <td colspan='2' class='center' style='padding:20px 0 20px 0;'>
	
	   <input type='submit' value=\"".__("Submit")."\" />
	
	  </td>
	
	 </tr>
	
	</table>
	
</form>";
}

ob_start();
?>
<script type='text/javascript' src='/js/jquery.js'></script>
<script type='text/javascript' src='/js/jquery.livequery.js'></script>
<script type='text/javascript' src='/js/jqtree/tree.js'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
	j('#genealogy_tree').SimpleTree({animate: true});
});
</script>
<style type="text/css">
.clear {
    clear: both;
}
</style>
<link rel='stylesheet' href='<?php echo JS_URL; ?>/jqtree/style.css' type='text/css'>
<?php
$jvscript = ob_get_clean();

$content="<h2>$page_title</h2>".$downline_box;
$content=str_replace("<%tab_content%>", $content, $tab);

print format_member_page($content, $this_title, $content_title, $css . $jvscript);
?>